package com.ziq.zcamp.mine

import android.content.Context
import android.content.Intent
import android.graphics.drawable.Animatable
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ScreenUtils
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.request.ImageRequest
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import com.ziq.base.base.BaseActivity
import com.ziq.zcamp.R
import com.ziq.zcamp.databinding.ActivityPicturePreviewBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.zcamp.util.ImageUtil
import me.relex.photodraweeview.PhotoDraweeView

class PicturePreviewActivity: BaseActivity() {
    companion object {
        const val PICTURE_LIST = "picture_list"
        const val PICTURE_SHOW_INDEX = "picture_show_index"
        fun open(context: Context, pictureList: ArrayList<String>, index:Int){
            val intent = Intent(context, PicturePreviewActivity::class.java)
            intent.putStringArrayListExtra(PICTURE_LIST, pictureList)
            intent.putExtra(PICTURE_SHOW_INDEX, index)
            GlobalContextManager.startActivity(context, intent)
        }
    }

    private val binding: ActivityPicturePreviewBinding by binding()
    var pictureList: List<String>? = null
    var initIndex = 0
    var screenRatio = 0f

    var screenWidth: Int = 0
    var screenHeight: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
        try {
            screenWidth = ScreenUtils.getScreenWidth()
            screenHeight = ScreenUtils.getScreenHeight()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        binding.root
        screenRatio = screenWidth.toFloat() * 1.0f / screenHeight.toFloat()
        try {
            if(intent.hasExtra(PICTURE_LIST)){
                pictureList  = intent.getStringArrayListExtra(PICTURE_LIST)
            }
            if(intent.hasExtra(PICTURE_SHOW_INDEX)){
                initIndex  = intent.getIntExtra(PICTURE_SHOW_INDEX, 0)
            }
        }catch (e:Exception){
            println("$e")
        }
        binding.back.click {
            finish()
        }

        binding.banner.setAdapter(ImageAdapter())
        binding.banner.setAutoPlay(false)
        binding.banner.setCanLoop(false)
        binding.banner.setOffScreenPageLimit(1)
        binding.banner.setIndicatorVisibility(View.GONE)
        binding.banner.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                updateBannerIndex(position)
            }
        })
        binding.banner.create(pictureList)
        if(initIndex >= pictureList!!.size){
            binding.banner.setCurrentItem(0, false)
            updateBannerIndex(0)
        } else {
            binding.banner.setCurrentItem(initIndex, false)
            updateBannerIndex(initIndex)
        }
    }

    fun updateBannerIndex(position: Int){
        if(pictureList?.size?:0 <= 1){
            binding.title.visibility = View.GONE
        } else {
            binding.title.visibility = View.VISIBLE
            binding.title.text = "${position + 1}/${pictureList?.size}"
        }
    }

    inner class ImageAdapter : BaseBannerAdapter<String>() {

        override fun getLayoutId(viewType: Int): Int {
            return R.layout.item_image_preview
        }

        override fun bindData(
            holder: BaseViewHolder<String>,
            data: String,
            position: Int,
            pageSize: Int
        ) {
            val photoView = holder.findViewById<PhotoDraweeView>(R.id.photo_view)
            photoView.maximumScale = 10f
            photoView.isEnableDraweeMatrix = false
            ImageUtil.updateImageFromNetwork(photoView, data, ImageUtil.imageMaxSize, ImageUtil.imageMaxSize, ImageUtil.imageMaxSize, ImageRequest.CacheChoice.DEFAULT,
                object : ImageUtil.MyBaseControllerListener() {
                    override fun onFailure(id: String?, throwable: Throwable?) {
                        super.onFailure(id, throwable)
                        photoView.isEnableDraweeMatrix = false
                    }

                    override fun onFinalImageSet(
                        id: String?, imageInfo: ImageInfo?,
                        animatable: Animatable?
                    ) {
                        super.onFinalImageSet(id, imageInfo, animatable)
                        photoView.isEnableDraweeMatrix = true
                        if (imageInfo != null) {
                            photoView.update(imageInfo.width, imageInfo.height)
                        }
                    }

                    override fun onIntermediateImageFailed(id: String?, throwable: Throwable?) {
                        super.onIntermediateImageFailed(id, throwable)
                        photoView.isEnableDraweeMatrix = false
                    }

                    override fun onIntermediateImageSet(id: String?, imageInfo: ImageInfo?) {
                        super.onIntermediateImageSet(id, imageInfo)
                        photoView.isEnableDraweeMatrix = true
                        if (imageInfo != null) {
                            photoView.update(imageInfo.width, imageInfo.height)
                        }
                    }
                })
        }

    }
}